<?php

namespace app\app\controller;

use app\app\model\ProjectDKModel;
use app\app\model\ProjectLZModel;
use app\app\model\ProjectModel;
use app\app\model\ProjectTowerModel;
use app\app\model\RandModel;
use app\app\model\UserModel;
use app\BaseController;
use app\Request;


class ProjectSignIn extends BaseController
{
    public function index(Request $request)
    {
        $data = $request->param();
        $page_size = input('pageSize', '20');
        $where = "1=1";
        if (isset($data['project_id'])) $where .= " AND a.project_id= '" . $data['project_id'] . "'";
        if (isset($data['user_name'])) $where .= " AND a.user_name LIKE '%" . $data['user_name'] . "%'";
//        if (isset($data['type'])) $where .= " AND a.type  like '%" . $data['type'] . "%'";
        if (isset($data['mark'])) $where .= " AND a.mark  = '" . $data['mark'] . "'";


        $list = ProjectDKModel::alias('a')->field('a.*,b.project_name')
            ->leftjoin('t_project b', 'b.id = a.project_id')
            ->order('dk_time_in desc')
            ->where($where)->paginate($page_size);
        $this->success('success', $list);
    }


    public function add(Request $request)
    {
        $data = $request->param();
        if (isset($data['id'])) {
            $dkModel = new \app\app\model\ProjectDKModel;
            $res = $dkModel->where('id', $data['id'])->find();
            if ($res) {
                unset($data['id']);
                $res->save($data);
            }
        } else {
            $user_info = UserModel::where('user_id', $data['user_id'])->find();
            $data['user_name'] = $user_info['nick_name'];
            $data['id'] = RandModel::randId();
            ProjectDKModel::create($data);
        }
        $this->success('打卡成功');
    }


//    public function add(Request $request)
//    {
//        $data = $request->param();
//        $user_info = UserModel::where('user_id', $data['user_id'])->find();
//        $data['user_name'] = $user_info['nick_name'];
//        $data['id'] = RandModel::randId();
//        $data['dk_time_in'] = date('Y-m-d H:i:s', time());
//        ProjectDKModel::create($data);
//
//        //用户当天未增加履职记录则增加履职记录
//        $where[] = ['project_id', '=', $data['project_id']];
//        $where[] = ['user_id', '=', $data['user_id']];
//        $start_time = date('Y-m-d 00:00:00', time());
//        $end_time = date('Y-m-d 23:59:59', time());
//        $where[] = ['lz_time', 'BETWEEN', [$start_time, $end_time]];
//        $info = ProjectLZModel::where($where)->select();
//
//        if(empty(count($info))){
//            $arr['id'] = RandModel::randId();
//            $arr['project_id'] = $data['project_id'];
//            $arr['user_id'] = $user_info['user_id'];
//            $arr['user_name'] = $user_info['nick_name'];
//            $arr['lz_time'] = date('Y-m-d H:i:s', time());
//            ProjectLZModel::create($arr);
//        }
//        $this->success('打卡成功');
//    }

    /**
     * 根据用户经纬度计算目的地的范围距离
     * @param $lat1
     * @param $lng1
     * @param $lat2
     * @param $lng2
     * @return float
     */
    public function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters
        $lat1 = ($lat1 * pi()) / 180;
        $lng1 = ($lng1 * pi()) / 180;
        $lat2 = ($lat2 * pi()) / 180;
        $lng2 = ($lng2 * pi()) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }

    /**
     * 打卡范围判断
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sign()
    {
        $data = $this->request->param();
        $towerList = ProjectTowerModel::where('project_id', $data['project_id'])->select();
        $latitude = $data['longitude'];
        $longitude = $data['latitude'];
        foreach ($towerList as $key => $value) {
            $float = $this->getDistance($data['latitude'], $data['longitude'], $value['latitude'], $value['longitude']);
            $float_old = $this->getDistance($latitude, $longitude, $value['latitude'], $value['longitude']);
            if ($float < 5000 || $float_old < 5000) {
                $this->success('已经进入打卡范围', 1);
            }
        }
        $this->success('未进入打卡范围', 0);
    }

    public function getLastProject()
    {
        $data = $this->request->param();
        $user_info = UserModel::where('user_name', $data['user_name'])->find();
        //如果上次有选择项目则显示上个项目
        if ($user_info['last_project_id']) {
            if ($data['duty'] == '项目经理') {
                $project_info = ProjectModel::where('link_user_id', 'like', $data['user_name'])->find();
            } else {
                $project_info = ProjectModel::find();
            }
        } else {
            //如果没有上次项目记录，根据职位查找自己名下项目
            if ($data['duty'] == '项目经理') {
                $project_info = ProjectModel::where('link_user_id', 'like', $data['user_name'])->find();
            } else {
                $project_info = ProjectModel::find();
            }
        }
        $this->success($project_info);
    }

}
